<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="用户ID" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入用户ID"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户名称" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入用户名称"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['fb:wallet:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          :disabled="multiple"
          @click="handleAdd"
          v-hasPermi="['fb:wallet:add']"
        >充值</el-button>
      </el-col>   
	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="walletList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="用户ID" align="center" prop="userId" />
      <el-table-column label="用户名称" align="center" prop="userName" :show-overflow-tooltip="true" />
      <el-table-column label="用户昵称" align="center" prop="nickName" :show-overflow-tooltip="true" />
      <el-table-column label="余额" align="center" prop="balance" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
                size="mini"
                type="text"
                icon="el-icon-search"
                @click="handleHistory(scope.row)"
                v-hasPermi="['fb:wallet:edit']"
              >充值记录</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      page.sync="1"
      limit.sync="10"
      @pagination="getList"
    />

    <!-- 添加或修改余额对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-table v-loading="loading" :data="historyList">
        <el-table-column label="交易ID" align="center" prop="tradeId" />
        <el-table-column label="交易金额" align="center" prop="payment" />
        <el-table-column label="交易时间" align="center" prop="tradeDime" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.tradeDime) }}</span>
          </template>
        </el-table-column>
      </el-table>
       <pagination
          v-show="totalHistory>0"
          :total="totalHistory"
          :page.sync="queryParamsHistory.pageNum"
          :limit.sync="queryParamsHistory.pageSize"
          @pagination="getHigstoryList"
        />
    </el-dialog>
  </div>
</template>

<script>
import { listWallet, getWallet, exportWallet,recharge } from "@/api/fb/wallet";
import { listInfo} from "@/api/fb/info";
export default {
  name: "Wallet",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 余额表格数据
      walletList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        userName: undefined,
        balance: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        userId: [
          { required: true, message: "用户ID不能为空", trigger: "blur" }
        ],
      },
      //充值记录
      historyList: [],
      // 总条数
      totalHistory: 0,
      // 查询参数
      queryParamsHistory: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        tradeType: 'chong'
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询余额列表 */
    getList() {
      this.loading = true;
      listWallet(this.queryParams).then(response => {
        this.walletList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        userId: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        balance: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.userId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有余额数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportWallet(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    },
    getHigstoryList(){
      this.historyList = [];
      listInfo(this.queryParamsHistory).then(response => {
        this.historyList = response.rows;
        this.totalHistory = response.total;
      });
    },
    //充值记录
    handleHistory(row){
      this.queryParamsHistory.userId=row.userId;
      this.getHigstoryList();
      this.open = true;
      this.title = "充值记录";

    },
    //充值框
    handleAdd() {
      this.$prompt('请输入充值金额', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        inputPattern: /^-?\d+$/,
        inputErrorMessage: '金额不正确'
      }).then(({ value }) => {
        recharge({"userIds":this.ids.join(),"amount":value}).then(response => {
          if(response.code==200){
            this.msgSuccess(response.msg);
            this.getList();
          }else{
            this.msgError(response.msg);
          }
        });
      }).catch(() => {
        //取消       
      });
    }
  }
};
</script>
